/**
 * 版权所有 2009-2015 中国中医科学院中医药信息研究所 何前锋 frontpeak@yahoo.com.cn
 **/
package cn.ac.cintcm.sh.dao;

import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

import cn.ac.cintcm.sh.domain.Chapter;

public class ChapterDao {
    private HsqldbDao dao = (new HsqldbDao()).getDao();
    private int maxChapterId;
    private int minChapterId;
    List<Chapter> chapters;
    
    public ChapterDao() {
        chapters=new ArrayList<Chapter>();
        String sql="select * from chapter";
        dao.openConnection();
        ResultSet rs=this.dao.executeQuery(sql);
        boolean firstRun = true;
        try{
            while(rs.next()){
                int id=rs.getInt("id");
                if (firstRun) {
                    minChapterId = id;
                    maxChapterId = id;
                    firstRun = false;
                }
                if (id < minChapterId) {
                    minChapterId = id;
                } else if (id > maxChapterId) {
                    maxChapterId = id;
                }
                String name=rs.getString("name");
                Chapter node=new Chapter();
                node.setId(id);
                node.setName(name);
                chapters.add(node);
            }
        }catch(Exception e){
            e.printStackTrace();
        }
        dao.closeConnection();
    }
    
    public List<Chapter> getChapters() {
 
        return chapters;
    }
    
    public boolean isFinalChapter(int chapterId) {
        
        return chapterId < this.maxChapterId && chapterId >= this.minChapterId;
    }

    public boolean isFirstChapter(int chapterId) {
        
        return chapterId <= this.maxChapterId && chapterId > minChapterId;
    }


}